// pages/TradingAreaCenter/activityCenter.js
const controller=require("../controllers/Controller").controller


const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:0,
    state:'',
    timeState:'',
    pageNumber:1,
    lastPage:false,
    list:[],
    tablist:['一卡通','欢乐拼','店铺爆款','限时抵扣','超值礼包'],
    currentIndex:0,
    search:"",
    timer:'',

    buttons: [{text: '失败'}, {text: '成功'}],
    shopCircleArray:[],
    dialogShow:false,
    couponBagId:'',
    failDesc:'',
    shopCircleName:'',
    shopGroupId:'',
  },
  
  handleInput(e){
    let search=e.detail.value;
    this.setData({
      search
    })
    if(this.timer)clearTimeout(this.timer)
    this.timer = setTimeout(() => {
      this.getBagPage()
    }, 500);
  },


  changeTabList(e){
    const eIndex = e.currentTarget.dataset.index
    let index=typeof eIndex === "number" ? eIndex : this.data.currentIndex;
    let type=''
    switch(index){
      case 0 :
        type=0;
        break;
      case 1 :
        type=3;
        break;
      case 2 :
        type=2;
        break;
      case 3 :
        type=4;
        break;
      case 4 :
        type=5;
    }
    this.setData({
      currentIndex:index,
      type
    }) 
    this.getBagPage()
  },

  //代理商管理券礼包列表 2.0
  getBagPage(){
    let value={}
    // value.pageNumber=1
    // value.state=this.data.state
    // value.timeState=''
    value.type=this.data.type
    value.name=this.data.search
    controller.getBagPage(value).then(res=>{
      let list = res.data
      list.forEach(item=>{
        switch(item.examineState){
          case 0:
            item.stateName = "待审核";
            item.stateColor = "yellow";
            break;
          case 1:
          case 3:
            item.stateName = "已通过";
            item.stateColor = "green";
            break;
          case 2:
            item.stateName = "未通过";
            item.stateColor = "red";
            break;
            // case 3:
            // item.stateName = "等待平台审核";
            // item.stateColor = "grey";
            // break;
          case 4:
            item.stateName = "审核失败";
            item.stateColor = "red";
            break;
        }
      })
      
      this.setData({
        list,
        // lastPage:res.data.lastPage
      })
    })
  },
  
  //审核
  examine(e){
    let id=e.detail.id
    this.setData({
      dialogShow:true,
      couponBagId:id
    })
  },
  tapDialogButton(e){
    let index=e.detail.index
    if(index===1){
      let value=new Object()
      value.shopGroupId=this.data.shopGroupId
      value.couponBagId=this.data.couponBagId
      if(!value.shopGroupId){
        wx.showToast({
          icon:"none",
          title: '请选择商圈',
          duration:1500
        })
        return
      }
      controller.examineSuccess(value).then(res=>{
        wx.showToast({
          icon:"none",
          title: '审核成功',
          duration:1500
        })
        this.setData({
          dialogShow:false
        })
        this.changeTabList()
      })
    }else{
      let value=new Object()
      value.couponBagId=this.data.couponBagId
      value.failDesc=this.data.failDesc
      if(!value.failDesc){
        wx.showToast({
          icon:"none",
          title: '请输入失败理由',
          duration:1500
        })
        return
      }
      controller.examineFail(value).then(res=>{
        wx.showToast({
          icon:"none",
          title: '审核失败已提交',
          duration:1500
        })
        this.setData({
          dialogShow:false
        })
        this.changeTabList()

      })
    }
    this.getBagPage()
  },
  bindPickerChange(e){
    let index=e.detail.value
    let list=this.data.shopCircleArray
    this.setData({
      shopCircleName:list[index].name,
      shopGroupId:list[index].id
    })
  },
  getReason(e){
    this.setData({
      failDesc:e.detail.value
    })
  },

   //获取商圈列表
   getShopCircleArray(){
    controller.myShopGroupList().then(res=>{
      this.setData({
        shopCircleArray:res.data
      })
    })
  },

  // 返回
  toTradingAreaCenter(){
    wx.redirectTo({
      url: '/pages/TradingAreaCenter/TradingAreaCenter',
    })
  },


  onLoad: function () {
    this.getBagPage()
    this.getShopCircleArray()
  },

  
})